package com.ygnn.controller;

import java.io.*;
import java.net.URISyntaxException;


import com.wechat.pay.contrib.apache.httpclient.exception.HttpCodeException;
import com.wechat.pay.contrib.apache.httpclient.exception.NotFoundException;
import com.ygnn.common.Dto.OrderDto;
import com.ygnn.common.Pojo.Order;
import com.ygnn.common.R.Result;
import com.ygnn.common.Vo.OrderVo;
import com.ygnn.common.Vo.WxPayRespVO;
import com.ygnn.common.context.BaseContext;
import com.ygnn.config.WxInformation;
import com.ygnn.service.OrderService;
import com.ygnn.service.WxPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.GeneralSecurityException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * ClassName:OrderController
 * Package:com.ygnn.controller
 * Description:
 *
 * @Author: dlh
 * @Creat:2024/10/8-22:18
 */
@RestController
@RequestMapping("/order")
@CrossOrigin
@Slf4j
@Api(value = "订单接口", tags = {"订单接口"})
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Resource
    private WxInformation wxInformation;
    @Resource
    private WxPayService wxPayService;

    @ApiOperation("新增订单")
    @PostMapping("/insert")
    public Result insert(@RequestBody OrderDto orderDto) throws ParseException, GeneralSecurityException, NotFoundException, IOException, URISyntaxException, HttpCodeException {

        //获取支付请求开关
        Boolean open = wxInformation.getOpen();
        //插入订单
        orderService.insert(orderDto, Order.NoPay);
        //调wx支付
        String description="跑跑特工";
        log.info("发起支付请求，商品描述：{}", description);
        log.info("发起支付请求，商品订单号：{}", orderDto.getOrderId());
        WxPayRespVO wxPayRespVO = wxPayService.nativePay(description, String.valueOf(1), String.valueOf(orderDto.getOrderId()));
        log.info("返回的实体参数： "+wxPayRespVO);
        return Result.success(wxPayRespVO);
    }
//    @ApiOperation("查询状态为0的所有订单")
//    @GetMapping("/selectAll")
//    public Result selectAll(){
//        List<OrderVo> orderVos = orderService.selectAll();
//        log.info("查询出状态为0的所有订单{}",orderVos);
//        return Result.success(orderVos);
//    }

    @ApiOperation("查询指定日期所有订单")
    @GetMapping("/selectByDate")
    public Result<List<OrderVo>> selectByDate(@RequestParam("date") @DateTimeFormat(pattern="yyyy-MM-dd") Date date) {
        List<OrderVo> orderVos = orderService.selectByDate(date);
        log.info("查询出{}的所有订单{}",date, orderVos);
        return Result.success(orderVos);
    }

    @ApiOperation("根据乐跑id查询订单")
    @GetMapping("/selectById/{id}")
    public Result<List<OrderVo>> selectById(@PathVariable int id) {
        List<OrderVo> orderVos = orderService.selectById(id);
        log.info("查询出id为：{}的订单：{}", id, orderVos);
        return Result.success(orderVos);
    }

    @ApiOperation("根据openid查询订单")
    @GetMapping("/selectByOpenid")
    public Result<List<OrderVo>> selectByOpenid() {
        String openid=BaseContext.getCurrentId();
        List<OrderVo> orderVos = orderService.selectByOpenid(openid);
        log.info("查询出openid为：{}的订单：{}", openid, orderVos);
        return Result.success(orderVos);
    }


}
